<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<meta name="renderer" content="webkit">
	    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
		<title></title>
		<link rel="stylesheet" href="__PUBLIC__/frame/layui/css/layui.css">
			<!--<link rel="stylesheet" href="http://cdn.datatables.net/1.10.13/css/jquery.dataTables.min.css">-->
		<link rel="stylesheet" href="__PUBLIC__/frame/static/css/style.css">
	</head>
	<body>
		<form class="layui-form" id="form1">
			<br/>
				<input type="hidden" name="cus_id" value="{$cid}" />
			     <div class="layui-form-item">
				    <div class="layui-inline">
					    <label class="layui-form-label">充值类型</label>
					    <div class="layui-input-block">
					      <select name="typeid" lay-verify="required" lay-filter="aihao">
					        <option value="">请选择</option>
					    <?php foreach ($mtype as $k => $v):?>
					        <option value="{$v['name']}">{$v['name']}</option>
					    <?php endforeach;?>
					      </select>
					    </div>
				    </div>
				    <div class="layui-inline">
				      <label class="layui-form-label">充值金额</label>
				      <div class="layui-input-inline">
				        <input type="text" name="OpFare" lay-verify="required" autocomplete="off" class="layui-input">
				      </div>
				    </div>
				    <div class="layui-inline">
				      <label class="layui-form-label">充值账号</label>
				      <div class="layui-input-inline">
				        <input type="text" name="number" lay-verify="required" autocomplete="off" class="layui-input">
				      </div>
				    </div>
		        </div>
		        <div class="layui-collapse" lay-filter="test">
		        	<div class="layui-colla-item">
		        		<h2 class="layui-colla-title">销售专员</h2>
		        		<div class="layui-colla-content">
		        			<div class="layui-form-item" id="fen">
		        				<span class="layui-form-label aa">[+]</span>
		        				<div class="layui-inline">
		        				<label class="layui-form-label">姓名：</label>
		        					<div class="layui-input-inline">
		        						<select name="sale_worker" lay-verify="required" lay-search="">
		        							<option value="">直接选择或搜索选择</option>
		        							<?php foreach ($data['worker'] as $k => $v):?>
		        								<option value="{$v['id']}">{$v['nickname']}</option>
		        							<?php endforeach;?>
		        						</select>
		        					</div>
		        				</div>
		        				<div class="layui-inline">
		        					<label class="layui-form-label">分成比例：</label>
		        					<div class="layui-input-inline">
		        						<input type="text" name="worker_percent" lay-verify="required|number" autocomplete="off" class="layui-input">
		        					</div>
		        				</div>
		        			</div>
		        		</div>
		        	</div>
		        	<div class="layui-colla-item">
		        		<h2 class="layui-colla-title">销售主任</h2>
		        		<div class="layui-colla-content">
		        			<div class="layui-form-item" id="fen">
		        				<span class="layui-form-label aa">[+]</span>
		        				<div class="layui-inline">
		        				<label class="layui-form-label">姓名：</label>
		        					<div class="layui-input-inline">
		        						<select name="sale_head" lay-verify="required" lay-search="">
		        							<option value="">直接选择或搜索选择</option>
		        							<?php foreach ($data['head'] as $k => $v):?>
		        								<option value="{$v['id']}">{$v['nickname']}</option>
		        							<?php endforeach;?>
		        						</select>
		        					</div>
		        				</div>
		        				<div class="layui-inline">
		        					<label class="layui-form-label">分成比例：</label>
		        					<div class="layui-input-inline">
		        						<input type="text" name="head_percent" lay-verify="required|number" autocomplete="off" class="layui-input">
		        					</div>
		        				</div>
		        			</div>
		        		</div>
		        	</div>
		        	<div class="layui-colla-item">
		        		<h2 class="layui-colla-title">销售经理</h2>
		        		<div class="layui-colla-content">
		        			<div class="layui-form-item" id="fen">
		        				<span class="layui-form-label aa">[+]</span>
		        				<div class="layui-inline">
		        				<label class="layui-form-label">姓名：</label>
		        					<div class="layui-input-inline">
		        						<select name="sale_manager" lay-verify="required" lay-search="">
		        							<option value="">直接选择或搜索选择</option>
		        							<?php foreach ($data['manager'] as $k => $v):?>
		        								<option value="{$v['id']}">{$v['nickname']}</option>
		        							<?php endforeach;?>
		        						</select>
		        					</div>
		        				</div>
		        				<div class="layui-inline">
		        					<label class="layui-form-label">分成比例：</label>
		        					<div class="layui-input-inline">
		        						<input type="text" name="manager_percent" lay-verify="required|number" autocomplete="off" class="layui-input">
		        					</div>
		        				</div>
		        			</div>
		        		</div>
		        	</div>
		        </div>
		        <div class="layui-form-item">  
				    <div class="layui-inline">
				      <label class="layui-form-label">操作时间</label>
				      <div class="layui-input-inline">
				        <input type="text" name="operate_time" id="date1" lay-verify="required" autocomplete="off" class="layui-input">
				      </div>
				    </div>
				  
		       </div>	
		       
		       <div class="layui-form-item layui-form-text">
				    <label class="layui-form-label">操作备注</label>
				    <div class="layui-input-block">
				      <textarea placeholder="请输入内容" name="remark" class="layui-textarea"></textarea>
				    </div>
				</div>
		       
				<div class="layui-form-item">
				    <div class="layui-input-block">
				      <button class="layui-btn" id="submit"  lay-filter="demo1">立即提交</button>
				      <button type="reset" class="layui-btn layui-btn-primary">重置</button>
				    </div>
				</div>
				
	    </form>
	</body>
<script type="text/javascript" src="__PUBLIC__/frame/layui/layui.js"></script>
	<script>
	layui.use(['form','element', 'jquery','layedit', 'laydate'], function(){
	  var form = layui.form
	  ,element = layui.element
	  ,		$= layui.jquery
	  ,layer = layui.layer
	  ,layedit = layui.layedit
	  ,laydate = layui.laydate;

	//监听折叠
	// element.on('collapse(test)', function(data){
	// 	layer.msg('展开状态：'+ data.show);
	// });
	  //日期选择
	  laydate.render({
      	elem: '#date1'
      });
      //业务分成
      $(document).on('click','#fen .aa',function(){
      	var add =  $(this).parent();
	  	if ($(this).html()=='[+]') {
	  		var newadd = add.clone();
	  		newadd.find('span').html('[-]');
	  		add.after(newadd);
	  		form.render();
	  	}else{
	  		add.remove();
	  	}
      });
      //获取窗口索引
      var index = parent.layer.getFrameIndex(window.name); 
      // 表单提交
      var reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/;
      $("input[name=OpFare]").blur(function(){
      	var typeid = $('select[name=typeid]').val();
      	var OpFare=$(this).val();
      	
        if(typeid=='退金'){
              if(OpFare>0){
                  layer.msg('资金格式错误1！');
                  return false;
              }else if(isNaN(OpFare)){
              	   layer.msg('请填写数字！');
                  return false;
              }

        }else{
        	 if(OpFare<0){
                  layer.msg('资金格式错误2！');
                  return false;
              }else if(isNaN(OpFare)){
              	   layer.msg('请填写数字！');
                  return false;
              }
        }
	  });
      $('#submit').click(function() {
      	var s={};
      	var p={};
      	var sw = [];
      	var pw = [];
      	var sh = [];
      	var ph = [];
      	var sm = [];
      	var pm = [];
      	var b={};
      	var status=false;
      	var sum=0;
      	var cus_id = $('input[name=cus_id]').val();
      	var typeid = $('select[name=typeid]').val();
      	var OpFare = $("input[name=OpFare]").val();
      	var number = $("input[name=number]").val();
      	//获取销售专员分成明细
      	var sale_worker = $("select[name=sale_worker]");
      		sale_worker.each(function(){
      			if($(this).val()!=''){
      				sw.push($(this).val());
      			}else{
      				layer.msg('业务员不能有空！');
      				return false;
      			}    			
      		});
      	var percent = $("input[name=worker_percent]");
      		percent.each(function(){
      			if($(this).val()!=''){
      				
      				pw.push($(this).val());
      				sum += parseFloat($(this).val());     				
      			}else{
      				layer.msg('分成比例不能有空！');
      				return false;
      			}    			
      		});
      		if(sum!=1){
      			layer.msg('所有分成比例和必须为1！');
      			return false;
      		}
      	s['worker']=sw;
      	p['worker']=pw;
      		sum=0;
      	//获取销售主任分成明细
      	var sale_worker = $("select[name=sale_head]");
      		sale_worker.each(function(){
      			if($(this).val()!=''){
      				
      				sh.push($(this).val());
      			}else{
      				layer.msg('销售主任不能有空！');
      				return false;
      			}    			
      		});
      	var percent = $("input[name=head_percent]");
      		percent.each(function(){
      			if($(this).val()!=''){
      				
      				ph.push($(this).val());
      				sum += parseFloat($(this).val());     				
      			}else{
      				layer.msg('主任分成比例不能有空！');
      				return false;
      			}    			
      		});
      		if(sum!=1){
      			layer.msg('主任所有分成比例和必须为1！');
      			return false;
      		}
      	s['head']=sh;
      	p['head']=ph;
      		sum=0;
      	//获取销售经理分成明细
      	var sale_worker = $("select[name=sale_manager]");
      		sale_worker.each(function(){
      			if($(this).val()!=''){
      			
      				sm.push($(this).val());
      			}else{
      				layer.msg('销售经理不能有空！');
      				return false;
      			}    			
      		});
      	var percent = $("input[name=manager_percent]");
      		percent.each(function(){
      			if($(this).val()!=''){
      				pm.push($(this).val());
      				sum += parseFloat($(this).val());     				
      			}else{
      				layer.msg('经理分成比例不能有空！');
      				return false;
      			}    			
      		});
      		if(sum!=1){
      			layer.msg('经理所有分成比例和必须为1！');
      			return false;
      		}
      	s['manager']=sm;
      	p['manager']=pm;
      	var operate_time = $("input[name=operate_time]").val();
      	var remark = $("textarea[name=remark]").val();
      	
      	if(typeid==''){
      		layer.msg('请选择操作类型！');
      		return false;
      	}
      	if(typeid=='退款'){
      		if(OpFare>0){
      			layer.msg('退款金额，请填负数！');
      			return false;
      		}      		    		
      	}else{
      		if(OpFare<0){
      			layer.msg('金额，请填正数！');
      			return false;
      		}
      	}
      	b['typeid']=typeid;
      	if(OpFare==''){
      		layer.msg('请填写操作资金！');
      		return false;
      	}
      	b['OpFare']=OpFare;
      	if(operate_time ==''){
      		layer.msg('请选择操作时间！');
      		return false;
      	}
      	b['operate_time']=operate_time;
      	b['cus_id']=cus_id;
      	b['remark']=remark;
      	b['number']=number;
      	layer.msg('提交中...', {
			icon: 16
			,shade: 0.01
		});
		$(this).addClass('layui-btn-disabled');	    	
	    $.post('/index.php/Hawk/Customer/moneyadd',{"sale_worker":s,"percent":p,"data":b},function(data){
	    	// console.log(data);
	    	var obj = eval('(' + data + ')');
	    	if(obj.code==1){
	    		parent.layer.close(index);
	    		parent.layer.msg(obj.msg);
	    	}else{
	    		layer.msg(obj.msg);
	    	}
	    	
	    	return false;
	    });
	    return false; 
	   }); 
	});		

	
	
</script>
</html>
